<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="到货单号">
        <el-input v-model="queryParams.params['daohuoNos']" placeholder="请输入到货单号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="到货日期">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="采购人" prop="lvBuycreateUsername">
        <el-input v-model="queryParams.lvBuycreateUsername" placeholder="请输入采购人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="制单人" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入制单人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="质检人" prop="zhijianName">
        <el-input v-model="queryParams.zhijianName" placeholder="请输入质检人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="收货人" prop="shourenName">
        <el-input v-model="queryParams.shourenName" placeholder="请输入收货人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择供应商" @change="handleQuery" @keyup.enter.native="handleQuery" filterable clearable>
          <el-option v-for="dict in dict.type.goods_supplier"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输产品编码"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输产品名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="产品型号" prop="goodModel">
        <el-select v-model="queryParams.goodModel" placeholder="请输入产品型号"
                   remote reserve-keyword :remote-method="searchGoods"   filterable clearable>
          <el-option v-for="item in goodsList"
                     :key="item.id" :label="item.label" :value="item.model"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" >导出</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              show-summary :summary-method="getSummaries"  :max-height="tableMaxHeight" @sort-change="handleSortChange">
      <el-table-column type="selection" width="60" align="center"/>
      <el-table-column sortable align="center"  label="到货单号" prop="lvDaohuo.nos">
        <template slot-scope="scope">
            <span @click="daohuoDetailByNos(scope.row)" style="color: #00afff;cursor:pointer;">
              {{scope.row.lvDaohuo.nos}}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="到货日期" align="center" prop="arrivalDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lvDaohuo.arrivalDate, '{y}-{m}-{d} {h}:{i}:{s}') || '' }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="采购人" align="center" prop="lvBuycreateUsername"/>
      <el-table-column sortable label="制单人" align="center" prop="createBy"/>
      <el-table-column sortable label="质检人" align="center" prop="zhijianName"/>
      <el-table-column sortable label="收货人" align="center" prop="shourenName"/>
      <el-table-column  sortable label="供应商" align="center" prop="supplierName"/>
      <el-table-column  label="客户名称" align="center" prop="goodsSalebuyall.customerName"/>
      <el-table-column  label="要货单位" align="center" prop="goodsSalebuyall.projectName"/>
      <el-table-column  label="产品编码" align="center" prop="goodsSalebuyall.goodsCode"/>
      <el-table-column  label="产品名称" align="center" prop="goodsSalebuyall.goodsName"/>
      <el-table-column  label="产品型号" align="center" prop="goodsSalebuyall.goodModel"/>
      <el-table-column  label="数量" align="center" prop="daohuoCount"/>
      <el-table-column  label="吨位" align="center" prop="daohuoDun"/>
      <el-table-column  label="单价" align="center" prop="unitPrice"/>
      <el-table-column  label="到货总价" align="center" prop="totalPrice"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-set-up"
                     @click="qualityTestingDaohuo(scope.row)" v-hasPermi="['develop:LvDaohuoDetail:edit']">质检情况
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <!-- <goods-salebuyLists ref="showsalebuyForm" @ok="getList"></goods-salebuyLists>-->
    <inspect-form ref="inspectForm" @ok="getList"></inspect-form>
    <factory-test-quality-daohuo ref="qualityTestingDaohuo" @ok="getList"></factory-test-quality-daohuo>
    <lv-daohuo-detail-list ref="daohuoDetailList" @ok="getList"></lv-daohuo-detail-list>
  </div>
</template>

<script>
import {accAdd, deleteAction, downloadFile, getAction, getById} from '@/api/manage'
import InspectForm from "@/views/develop/lvBuy/module/ZhijianForm";
import {list} from "@/api/develop/lvDaohuoDetail";
import FactoryTestQuality from "@/views/develop/factory/factoryTestQuality/FactoryTestQuality.vue";
import FactoryTestQualityAll from "@/views/develop/factory/factoryTestQuality/FactoryTestQualityAll.vue";
import LvDaohuoDetailList from "@/views/develop/lvDaohuoDetail/module/LvDaohuoDetailList.vue";
import FactoryTestQualityDaohuo from "@/views/develop/factory/factoryTestQuality/FactoryTestQualityDaohuo.vue";

export default {
  name: "ZhijianList",
  dicts: ['inspect_state', 'goods_supplier'],
  components: {
    FactoryTestQualityDaohuo,
    LvDaohuoDetailList,
    FactoryTestQualityAll,
    FactoryTestQuality,
    // GoodsSalebuyLists,
    InspectForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购单表格数据
      dataList: [],
      goodsList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nos: null,
        arrivalDate: null,
        supplierName: null,
        goodsCode: null,
        goodsName: null,
        goodModel: null,
        status: "7",
        lvBuycreateUsername: null,
        createBy: null,
        zhijianName: null,
        shourenName: null,
        orderByColumn: "createTime",
        isAsc: "desc",
        params:{}
      },
      url: {
        list: '/lvDaohuoDetail/lvDaohuoDetail/list',
        zhijianwancheng: '/goodsSalebuyall/goodsSalebuyall/zhijianwancheng/',
        zhijianDaohuoDetail: '/lvDaohuoDetail/lvDaohuoDetail/zhijianDaohuoDetail',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {

    daohuo(row) {
      this.$refs.showsalebuyForm.showsalebuyList(row);
    },
    /** 查询采购单列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange,"ArrivalDate")).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.params={};
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    inspect(row) {
      this.$refs.inspectForm.inspectShow(row);
    },
    /** 质检完成更新GoodsSalebuyall*/
    handleZhijian(state) {
      const ids = this.ids;
      //let params ={"ids":this.ids.toString(),"state":state}
      deleteAction(this.url.zhijianwancheng, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("操作成功！");
      }).catch(() => {
      });
    },
    zhijianDaohuoDetail(state) {
      let params = {"ids": this.ids.toString(), "state": state}
      getAction(this.url.zhijianDaohuoDetail, params).then(response => {
        this.getList();
        this.$modal.msgSuccess("操作成功！");
      }).catch(() => {
      });
    },

    qualityTestingDaohuo(row) {
      this.$refs.qualityTestingDaohuo.showByDaohuoDetail(row);
    },

    /** 导出按钮操作 */
    handleExport() {
      let that = this;
      that.loading = false;
      that.$confirm('是否确认导出产品到货明细?', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        downloadFile('/lvDaohuoDetail/lvDaohuoDetail/export', that.queryParams);
      });
    },

    searchGoods(queryLabel) {
      if (queryLabel !== '') {
        let params = {"queryLabel": queryLabel, "pageNum": 1, "pageSize": 50,};
        getAction('/goodsOrderIn/goodsOrderIn/list', params).then(response => {
          this.goodsList = response.rows;
          for (let i = 0; i < this.goodsList.length; i++) {
            this.goodsList[i].label = this.goodsList[i].name + '-' + this.goodsList[i].model + '-' + this.goodsList[i].code;
          }
        })
      } else {
        this.goodsList = [];
      }
    },


    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "daohuoCount" || column.property === "daohuoDun" || column.property === "totalPrice") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },


  }
};
</script>
